let app = getApp();
let httptool=require("../../../utils/zry.httptool.js");
Page({
  data:{
      circleList:[],//我的圈子
      topics:[],//个人的帖子
      page: { PageIndex: 0, PageSize: 10 },
      bgcover:true,//背景遮罩层是否显示
      peopCheckVal:'',//转让时选择的人员 转让选择的用户id
      peopselect:true,//人员列表是丰富显示
      peoplist:[],//转让人权限列表
      pop_cont:'',
      solveQuit:true,//解散退出擂盟区域显示
      paramQuit:{},
      transfer:{},
      defCircle:'',//默认圈子id
      imgUrl:'../../../images/no_def.png',//非默认擂盟
      transferID:'',//转让的擂盟id
      msgprop:'',//消息提示 
      delArea:true,//删除提示框
      delforums:{},//删除帖子是需要的参数
      index:0,
      nodataMsg:''
  },
  peopCheckedChange:function(e){
    let that=this;
    this.setData({
      peopCheckVal: e.detail.value,
      receiveId: e.detail.value
    })
    console.log("peopCheckVal id是："+ this.data.peopCheckVal);
  },
  giveBtn:function(e){//放弃
    let that=this;
    that.setData({bgcover:true,solveQuit:true});
  },
  giveBtns:function(e){//放弃
    let that=this;
    that.setData({bgcover:true,peopselect:true});
  },
  popConfirm:function(e){//解散退出确定
    let that=this;
    that.quitCircle();
  },
  popConfirm_tran:function(e){//转让确定
    let that=this;
    that.transOther();
  },
  setDefault:function(e){//设置默认的圈子
    let that=this;
    let circleParam={};
    circleParam.CircleId=e.currentTarget.dataset.id;
    circleParam.ID=app.getStorage('customerId');
    let index= e.currentTarget.dataset.index;
    let src=e.currentTarget.dataset.src;
    httptool.ajaxPost("api/Customer/ChangeDefaultCircleId", circleParam,function(res){//设置默认圈子
      if(res.suc){
        if(that.data.circleList[index].ID==that.data.defCircle){
             that.data.circleList[index].imgUrl='../../../images/default_c.png';
          }
          let page = { PageIndex: 0, PageSize: 10 };
          that.setData({ page: page });
          that.getCircleList();
          that.getPersonForum();
      }else{
        app.showNotify(that, res.msg);
      }
    });
  },
  giveOther:function(e){//转让
    let that=this;
    let circleid=e.currentTarget.dataset.circleid;
    that.setData({bgcover:false,peopselect:false,transferID:circleid});
    httptool.ajaxPost("api/Circle/GetTransfer", {ID: circleid},function(res){//获取转让人权限列表
      if(res.suc){
          let data = app.notNull(res.data, []);
          if(data.length>0){
            that.setData({peoplist: data});
          }
      }else{
        app.showNotify(that, res.msg);
      }
    });
  },
  dissolve:function(e){//解散,退出
    let that=this;
    let types=e.currentTarget.dataset.types;
    that.setData({bgcover:false,solveQuit:false});
    console.log(types);
    if(types==1){
      that.setData({pop_cont:'解散本盟数据不可恢复，是否解散'});
    }else if(types==2){
      that.setData({pop_cont:'你确定要退出该擂盟?'});
    }
    let paramQuit={};
    paramQuit.ID=e.currentTarget.dataset.circleid;
    paramQuit.UserId=app.getStorage('customerId');
    that.setData({paramQuit:paramQuit});
  },
  quitCircle:function(){//退出，解散擂盟确定时的接口
    let that=this;
    httptool.ajaxPost("api/Circle/ExitCircle", that.data.paramQuit,function(res){//退出解散转
      if (res.suc) {
        let page = { PageIndex: 0, PageSize: 10 };
        that.setData({ bgcover: true, solveQuit: true, page: page});
        that.getCircleList();
        that.getPersonForum();
      }else{
        app.showNotify(that, res.msg);
      }
    });
  },
  transOther:function(){
      let that=this;
      let transfer={};
      transfer.ID=that.data.transferID;
      transfer.ToUserId=that.data.peopCheckVal;
      transfer.UserId=app.getStorage('customerId');
      that.setData({transfer:transfer});
      if(transfer.ToUserId !='' && transfer.ToUserId !=null && transfer.ToUserId !=undefined){
          httptool.ajaxPost("api/Circle/AddTransfer", that.data.transfer,function(res){//擂盟转让确定时的接口
            if (res.suc) {
              let page = { PageIndex: 0, PageSize: 10 };
              that.setData({ bgcover: true, peopselect: true, page: page});
              wx.showToast({
                  title: "转让成功",
                  duration: 1000,
                  icon: "success"
                })
              that.getCircleList();
              that.getPersonForum();
          }else{
            app.showNotify(that, res.msg);
          }
        });
      }else{
         app.showNotify(that, "请先选择一个接收人！");
      }

  },
 enterDetail: function (e) {//点击帖子进入对应的帖子详情
    let forumId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/detailPost/detailPost?ID=' + forumId,
    })
  },
  friendDetail:function(e){//点击头像进入某人的详情页，查看对方信息，帖子
    let that=this;
    let ToUserId=e.currentTarget.dataset.creatid;
    wx.navigateTo({
      url: '/pages/followdetail/followdetail?ToUserId='+ToUserId,
    });
  },
  onLoad:function(options){
    let that = this;
    httptool.login(function () {
      that.getCircleList();
      let page = { PageIndex: 0, PageSize: 10 };
      that.setData({ topics: [], page: page });
      that.getPersonForum();
    });
  },
  onShow: function () {
    let that = this;
    
  },
  onPullDownRefresh: function () {//下拉刷新
    let that = this;
    let page = { PageIndex: 0, PageSize: 10 };
    that.setData({ topics: [], page: page });
    that.getPersonForum();
    wx.stopPullDownRefresh();
  },
  onReachBottom: function () {//上拉加载
    let that = this;
    that.getPersonForum();
  },
  getCircleList:function(){//圈子列表
    let that = this;
    let customerId = app.getStorage('customerId');
    httptool.ajaxPost("api/Circle/GetMyCircles", { UserId: customerId},function(res){//获取我的圈子列表数据
      if(res.suc){
          let data = app.notNull(res.data.DataList, []);
          let defCircle=res.data.DefaultCircle;
          for(let i=0;i<data.length;i++){
              let arr1=data[i].CreateDate.split(' ');
              let arr2=arr1[0].substring(5);
              let arr3=arr1[1].substring(0,5);
              data[i].CreateDate=arr2+' '+ arr3;
              console.log(arr1,arr2,arr3);
              data[i].imgUrl='../../../images/no_def.png';
            if(data[i].ID==defCircle){
              data[i].imgUrl='../../../images/default_c.png';
            }else{
              data[i].imgUrl='../../../images/no_def.png';
            }
          } 
          that.setData({defCircle:defCircle, circleList: data});
      }else{
        app.showNotify(that, res.msg);
      }
    });
  },
  getPersonForum:function(){//获取个人帖子
      let that=this;
      let param={};
      param.UserId=app.getStorage('customerId');
      param.PageIndex = that.data.page.PageIndex + 1;
      param.PageSize = that.data.page.PageSize;
      app.showLoading();
      httptool.ajaxPost("api/Topic/GetFriendTopic", param, function (res) {
        if (param.PageIndex==1){
          that.setData({ topics: [] });
        }
        that.backTopics(res, that);
      },function () { 
          wx.hideLoading(); 
      });
  },
  backTopics: function (res,that){
    if (res.suc) {
      let data = app.notNull(res.data, []);
      if (data.length > 0) {
        that.data.page.PageIndex += 1;
        for(let i in data){
          data[i].CreateDate=app.editTime(data[i].CreateDate);
          data[i].IsDels=true;
        }
        data = that.data.topics.concat(data);
        that.setData({ topics: data, page: that.data.page });
      }
    } else {
      app.showNotify(that, res.msg);
    }
    that.setData({ nodataMsg: that.data.topics.length > 0 ? "" : "暂无帖子..." });
    wx.hideLoading();
  },
  thumbs_up: function (e) {//点赞事件
    let that = this;
    let isup=e.currentTarget.dataset.isup;
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let likeTimes = e.currentTarget.dataset.liketimes;
    let thumbs = {};
    thumbs.UserId =app.getStorage('customerId');
    thumbs.TopicId = id;
      httptool.ajaxPost("api/TopicZan/AddorDelLike", thumbs, function (res) {
        if (res.suc) {
          if(res.data==1){
            that.data.topics[index].LikeTimes = likeTimes + 1;
            that.setData({ topics: that.data.topics });
            wx.showToast({
              title: "点赞成功",
              duration: 1000,
              icon: "success"
            })
          }else if(res.data==0){
            that.data.topics[index].LikeTimes = likeTimes - 1;
            that.setData({ topics: that.data.topics });
            wx.showToast({
              title: "取消点赞",
              duration: 1000,
              icon: "success"
            })
          }
        } else {
          app.showNotify(that, res.msg);
        }
      });
  },
  enterForward:function(e){//点击进入转发页面
    let that=this;
    let fourumId=e.currentTarget.dataset.id;
    let Reference=e.currentTarget.dataset.reference;
    if(Reference!=0){
      wx.navigateTo({
        url: '/pages/forward/forward?fourumId='+Reference,
      });
    }else{
      wx.navigateTo({
        url: '/pages/forward/forward?fourumId='+fourumId,
      });
    }
  },
  goToforwardForum:function(e){//点击转发的帖子内容进入对应的帖子详情
    let ID=e.currentTarget.dataset.forumid;
    wx.navigateTo({
      url: '/pages/detailPost/detailPost?ID=' + ID,
    });
  },
  enterCircle:function(e){//点击进入对应的擂盟详情
    let id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/search/search?ID='+id+"&eId="+1,
    });
  },
  enterComment:function(e){////点击评论按钮进入对应的帖子详情，并调出回复框
    let forumId = e.currentTarget.dataset.id;
    let UserId = app.getStorage('customerId') || '0';
    wx.navigateTo({
      url: '/pages/detailPost/detailPost?ID=' + forumId+"&dtype="+1,
    })
  },
  previewImg:function(e){//预览图片
    let src = e.target.dataset.src;//获取data-src
    let imgLists = e.currentTarget.dataset.list;//获取data-list
      //图片预览
     wx.previewImage({
       current: src, // 当前显示图片的http链接
       urls: imgLists // 需要预览的图片http链接列表
    })
  },
  reSend:function(e){//编辑
    let reference=e.currentTarget.dataset.reference;//是否是对转发的帖子进行编辑
    let fourumId=e.currentTarget.dataset.id;//帖子id
    if(reference==0){
      wx.navigateTo({
        url: '/pages/share/share?etype='+1+'&topicId='+fourumId,
      })
    }else{
      wx.navigateTo({
        url: '/pages/forward/forward?etype='+1+'&fourumId='+fourumId+"&reference="+reference,
      })
    }

  },
  delForum:function(e){//删除帖子按钮
     let that=this;
     let delforums={};
     let index = e.currentTarget.dataset.index;
     delforums.ID=e.currentTarget.dataset.id;
     delforums.UserId=app.getStorage('customerId');
     that.setData({delforums:delforums,delArea:false,index:index});
  },
  delForumaction:function(e){//删除帖子
     let that=this;
     let index=that.data.index;
     httptool.ajaxPost("api/Topic/DelTopic",that.data.delforums, function (res) {
        if (res.suc) {
          wx.showToast({
            title: '删除成功',
            icon: 'success',
            duration: 2000
          });
          that.data.topics.splice(index,1);
          that.setData({ topics: that.data.topics,delArea:true}); 
        }else{
              app.showNotify(that, res.msg);
            }
        });
  },
    /** 删除帖子时**/
  closeDel:function(e){
    let that=this;
    that.setData({delArea:true});
  },
  canDel:function(e){//取消
    let that=this;
    that.setData({delArea:true});
  },
  delAction:function(e){//确定删除
    let that=this;
    that.delForumaction();
  },
  /** 删除帖子结束**/
})